<HTML>
<BODY BGCOLOR="white">
<PRE>
<FONT color="green">001</FONT>    package net.jmatrix.eproperties.include;<a name="line.1"></a>
<FONT color="green">002</FONT>    <a name="line.2"></a>
<FONT color="green">003</FONT>    import java.lang.reflect.Method;<a name="line.3"></a>
<FONT color="green">004</FONT>    import java.util.*;<a name="line.4"></a>
<FONT color="green">005</FONT>    <a name="line.5"></a>
<FONT color="green">006</FONT>    import net.jmatrix.eproperties.EProperties;<a name="line.6"></a>
<FONT color="green">007</FONT>    <a name="line.7"></a>
<FONT color="green">008</FONT>    import org.apache.commons.logging.*;<a name="line.8"></a>
<FONT color="green">009</FONT>    <a name="line.9"></a>
<FONT color="green">010</FONT>    /**<a name="line.10"></a>
<FONT color="green">011</FONT>     *<a name="line.11"></a>
<FONT color="green">012</FONT>     */<a name="line.12"></a>
<FONT color="green">013</FONT>    public class MethodPropertiesLoader implements PropertiesLoader {<a name="line.13"></a>
<FONT color="green">014</FONT>       static Log log=LogFactory.getLog(MethodPropertiesLoader.class);<a name="line.14"></a>
<FONT color="green">015</FONT>       <a name="line.15"></a>
<FONT color="green">016</FONT>       // FIXME: Add method execution URL handler.  Like this:<a name="line.16"></a>
<FONT color="green">017</FONT>       // [method://java.net.InetAddress.getLocalHost().getHostName()]<a name="line.17"></a>
<FONT color="green">018</FONT>       // this would return a String rather than a nested properties<a name="line.18"></a>
<FONT color="green">019</FONT>       // object.  Which could be difficult.  <a name="line.19"></a>
<FONT color="green">020</FONT>       // Could make some implicit assumptions about the method:// processor<a name="line.20"></a>
<FONT color="green">021</FONT>       //   - Strings would be single value<a name="line.21"></a>
<FONT color="green">022</FONT>       //   - List or String[] would be converted to List&lt;String&gt; using toString?<a name="line.22"></a>
<FONT color="green">023</FONT>       //   - Properties or Map or Hashtable would be convered to nested EProperties()<a name="line.23"></a>
<FONT color="green">024</FONT>       //   - Object could be converted to an EProperties object by introspecting<a name="line.24"></a>
<FONT color="green">025</FONT>       //     for public accessors and/or methods<a name="line.25"></a>
<FONT color="green">026</FONT>       //     This could be interesting.  So if a method returned URL, <a name="line.26"></a>
<FONT color="green">027</FONT>       //     we could either use url.toString(), or <a name="line.27"></a>
<FONT color="green">028</FONT>       //     introspect into URL and getFile(), getHost(), getPort(), and getProtocol() would<a name="line.28"></a>
<FONT color="green">029</FONT>       //     result in:<a name="line.29"></a>
<FONT color="green">030</FONT>       //     foo={<a name="line.30"></a>
<FONT color="green">031</FONT>       //        file=(result from getFile())<a name="line.31"></a>
<FONT color="green">032</FONT>       //        host=(result from getHost())<a name="line.32"></a>
<FONT color="green">033</FONT>       //        # etc, you get the picture.<a name="line.33"></a>
<FONT color="green">034</FONT>       //     }<a name="line.34"></a>
<FONT color="green">035</FONT>       <a name="line.35"></a>
<FONT color="green">036</FONT>       /** */<a name="line.36"></a>
<FONT color="green">037</FONT>       public boolean acceptsURL(String s) {<a name="line.37"></a>
<FONT color="green">038</FONT>          if (s.toLowerCase().startsWith("method://")) {<a name="line.38"></a>
<FONT color="green">039</FONT>             return true;<a name="line.39"></a>
<FONT color="green">040</FONT>          }<a name="line.40"></a>
<FONT color="green">041</FONT>          return false;<a name="line.41"></a>
<FONT color="green">042</FONT>       }<a name="line.42"></a>
<FONT color="green">043</FONT>    <a name="line.43"></a>
<FONT color="green">044</FONT>       /** */<a name="line.44"></a>
<FONT color="green">045</FONT>       public Object loadProperties(EProperties parent, String url, <a name="line.45"></a>
<FONT color="green">046</FONT>             EProperties options) {<a name="line.46"></a>
<FONT color="green">047</FONT>          EProperties props=new EProperties(parent);<a name="line.47"></a>
<FONT color="green">048</FONT>          //props.addAll(System.getProperties());<a name="line.48"></a>
<FONT color="green">049</FONT>          <a name="line.49"></a>
<FONT color="green">050</FONT>          // parse the URL: <a name="line.50"></a>
<FONT color="green">051</FONT>          // strip off method://<a name="line.51"></a>
<FONT color="green">052</FONT>          <a name="line.52"></a>
<FONT color="green">053</FONT>          url=url.substring("method://".length());<a name="line.53"></a>
<FONT color="green">054</FONT>          <a name="line.54"></a>
<FONT color="green">055</FONT>          // java.lang.System.getenv()<a name="line.55"></a>
<FONT color="green">056</FONT>          <a name="line.56"></a>
<FONT color="green">057</FONT>          // parse the classs name and method name.<a name="line.57"></a>
<FONT color="green">058</FONT>          String className=url.substring(0, url.lastIndexOf("."));<a name="line.58"></a>
<FONT color="green">059</FONT>          String methodName=url.substring(url.lastIndexOf(".")+1);<a name="line.59"></a>
<FONT color="green">060</FONT>          <a name="line.60"></a>
<FONT color="green">061</FONT>          if (methodName.endsWith("()"))<a name="line.61"></a>
<FONT color="green">062</FONT>             methodName=methodName.substring(0, methodName.length()-2);<a name="line.62"></a>
<FONT color="green">063</FONT>          <a name="line.63"></a>
<FONT color="green">064</FONT>          try {<a name="line.64"></a>
<FONT color="green">065</FONT>             Class clazz=Class.forName(className);<a name="line.65"></a>
<FONT color="green">066</FONT>             <a name="line.66"></a>
<FONT color="green">067</FONT>             Method method=clazz.getMethod(methodName);<a name="line.67"></a>
<FONT color="green">068</FONT>             <a name="line.68"></a>
<FONT color="green">069</FONT>             Object obj=method.invoke(null);<a name="line.69"></a>
<FONT color="green">070</FONT>             <a name="line.70"></a>
<FONT color="green">071</FONT>             if (obj == null) {<a name="line.71"></a>
<FONT color="green">072</FONT>                log.error("Method include of "+className+"."+methodName+" returned null.");<a name="line.72"></a>
<FONT color="green">073</FONT>             }<a name="line.73"></a>
<FONT color="green">074</FONT>             <a name="line.74"></a>
<FONT color="green">075</FONT>             if (obj instanceof Map) {<a name="line.75"></a>
<FONT color="green">076</FONT>                Map map=(Map)obj;<a name="line.76"></a>
<FONT color="green">077</FONT>                <a name="line.77"></a>
<FONT color="green">078</FONT>                Set keys=map.keySet();<a name="line.78"></a>
<FONT color="green">079</FONT>                int count=0;<a name="line.79"></a>
<FONT color="green">080</FONT>                for(Object key:keys) {<a name="line.80"></a>
<FONT color="green">081</FONT>                   count++;<a name="line.81"></a>
<FONT color="green">082</FONT>                   Object value=map.get(key);<a name="line.82"></a>
<FONT color="green">083</FONT>                   <a name="line.83"></a>
<FONT color="green">084</FONT>                   props.put(key.toString(), value.toString());<a name="line.84"></a>
<FONT color="green">085</FONT>                }<a name="line.85"></a>
<FONT color="green">086</FONT>                <a name="line.86"></a>
<FONT color="green">087</FONT>                log.debug("Loaded "+count+" properties with MethodLoader.");<a name="line.87"></a>
<FONT color="green">088</FONT>             } else {<a name="line.88"></a>
<FONT color="green">089</FONT>                log.error("Don't know how to process method include of object type "+obj.getClass().getName());<a name="line.89"></a>
<FONT color="green">090</FONT>             }<a name="line.90"></a>
<FONT color="green">091</FONT>          } catch (Exception ex) {<a name="line.91"></a>
<FONT color="green">092</FONT>             throw new RuntimeException("Error processing method:// inclusion.", ex);<a name="line.92"></a>
<FONT color="green">093</FONT>          }<a name="line.93"></a>
<FONT color="green">094</FONT>          <a name="line.94"></a>
<FONT color="green">095</FONT>          //if (true)<a name="line.95"></a>
<FONT color="green">096</FONT>          //   throw new RuntimeException("Error method:// inclusion not implemented.");<a name="line.96"></a>
<FONT color="green">097</FONT>          <a name="line.97"></a>
<FONT color="green">098</FONT>          return props;<a name="line.98"></a>
<FONT color="green">099</FONT>       }<a name="line.99"></a>
<FONT color="green">100</FONT>    }<a name="line.100"></a>




























































</PRE>
</BODY>
</HTML>
